<?php

namespace app\api\controller;

use app\admin\model\UserConfigChargeModel;
use app\admin\model\UserConfigHxModel;
use app\admin\model\UserConfigModel;
use app\common\controller\Api;


class UserConfig extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * @var UserConfigModel
     */
    protected $model = null;

    protected function _initialize()
    {
        parent::_initialize();
        $this->model = new UserConfigModel();
    }

    public function _empty($name)
    {
        $this->do($name);
    }

    private function do($type)
    {
        $param = $this->request->param();
        if ($this->request->isPost()) {
            if (!empty($param['backup_email']) && !\think\Validate::is($param['backup_email'], 'email')) {
                $this->error('邮箱格式不正确');
            }
            $this->model->saveUserConfig($this->auth->id, $type, $param);
            $this->success('保存成功');
        }
        $data = $this->model->getUserConfig($this->auth->id, $type);
        $list = $this->model->getList($type);
        foreach ($list as $k => $v) {
            $list[$k]['value'] = $data[$v['field']] ?? '';
        }
        $this->success('成功', $list);
    }

    public function deposit_type()
    {
        $this->success('成功', $this->model->getDepositTypeList());
    }

    public function charge_list()
    {
        $kw = $this->request->param('kw');
        $where = [];
        if (!empty($kw)) {
            $where['name'] = ['like', '%' . $kw . '%'];
        }
        $where['user_id'] = $this->auth->id;
        $list = UserConfigChargeModel::where($where)
            ->field(['id', 'name', 'money', 'pay_type'])
            ->select();
        foreach ($list as $item) {
            $item->append(['pay_type_text']);
        }

        $this->success('ok', $list);
    }

    public function charge_add()
    {
        $params = $this->request->post();
        $checkRes = $this->validate($params, [
            'name|名称' => 'require',
            'money|价格' => 'require',
            'pay_type|支付方式' => 'require',
        ]);
        if ($checkRes !== true) {
            $this->error($checkRes);
        }
        $params['user_id'] = $this->auth->id;
        $res = UserConfigChargeModel::create($params, true);

        if ($res) {
            $this->success('保存成功');
        } else {
            $this->error('保存失败');
        }
    }

    public function charge_edit()
    {
        $params = $this->request->post();
        $checkRes = $this->validate($params, [
            'id|ID' => 'require',
            'name|名称' => 'require',
            'money|价格' => 'require',
            'pay_type|支付方式' => 'require',
        ]);
        if ($checkRes !== true) {
            $this->error($checkRes);
        }
        $row = UserConfigChargeModel::get($params['id']);
        if (!$row) {
            $this->error('数据不存在');
        }
        $res = $row->allowField(true)->save($params);
        if ($res) {
            $this->success('保存成功');
        } else {
            $this->error('保存失败');
        }
    }


    public function hx_list()
    {
        $kw = $this->request->param('kw');
        $where = [];
        if (!empty($kw)) {
            $where['name'] = ['like', '%' . $kw . '%'];
        }
        $where['user_id'] = $this->auth->id;
        $list = UserConfigHxModel::where($where)
            ->field(['id', 'name', 'room_num', 'kt_num' , 'toilet_num' , 'sort_num', 'type'])
            ->order(['sort_num' => 'asc', 'id' => 'desc'])
            ->select();

        $this->success('ok', $list);
    }
    public function hx_del()
    {
        $id = $this->request->param('id');
        if(!$id){
            $this->error("缺少必传参数");
        }
        $where['id'] = $id;
       UserConfigHxModel::where($where)->delete();

        $this->success('删除成功');
    }
    public function hx_add()
    {
        $params = $this->request->post();
        $checkRes = $this->validate($params, [
            'name|名称' => 'require',
            'room_num|房间数' => 'require',
            'type|类型' => 'require|in:1,2',
        ]);
        if ($checkRes !== true) {
            $this->error($checkRes);
        }
        $params['user_id'] = $this->auth->id;
        $res = UserConfigHxModel::create($params, true);

        if ($res) {
            $this->success('保存成功');
        } else {
            $this->error('保存失败');
        }
    }

    public function hx_edit()
    {
        $params = $this->request->post();
        $checkRes = $this->validate($params, [
            'id|ID' => 'require',
            'name|名称' => 'require',
            'room_num|房间数' => 'require',
            'type|类型' => 'require|in:1,2',
        ]);
        if ($checkRes !== true) {
            $this->error($checkRes);
        }
        $row = UserConfigHxModel::get($params['id']);
        if (!$row) {
            $this->error('数据不存在');
        }
        $res = $row->allowField(true)->save($params);
        if ($res) {
            $this->success('保存成功');
        } else {
            $this->error('保存失败');
        }
    }
}
